package cn.edu.hqu.example.bj.experiment_2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;

public class GridLayoutActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_layout);

        String[] chars = new String[]
                {
                        "1","2","3",
                        "4","5","6",
                        "7","8","9",
                        "0","+","-",
                        "*","/","="
                };
        GridLayout gridLayout;
        gridLayout = (GridLayout) findViewById(R.id.root);
        for (int i = 0; i < chars.length; i++)
        {
            Button button =new Button(this);
            button.setText(chars[i]);
            button.setTextSize(40);
            button.setPadding(5,35,5,35);

            GridLayout.Spec roSpec = GridLayout.spec(i/3);
            //assign the column number of this components
            GridLayout.Spec columnSpec = GridLayout.spec(i%3);
            GridLayout.LayoutParams params =new GridLayout.LayoutParams(roSpec,columnSpec);
            //assign this components full parent container
            params.setGravity(Gravity.FILL);
            gridLayout.addView(button,params);
        }
        Button bt_17=findViewById(R.id.bt_17);
        bt_17.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(GridLayoutActivity.this,MainActivity.class);
                startActivity(i);
            }
        });
    }
}
